package com.woniuxy.cloud.starter.logging;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.util.StopWatch;

/**
 * 接口执行日志切面类,前提：（1）需要确保类路径存在starer-aop和logback。（2）自行注册到spring容器
 * @author liwei,lucas
 *
 */
@Aspect
@Slf4j
public class LogAspect {

	/**
	 * 通过环绕通知在接口执行前后输出日志信息
	 * @param proceedingJoinPoint 
	 * @return
	 * @throws Throwable
	 */
	@Around("@annotation(com.woniuxy.cloud.starter.logging.Log)")
	public Object logOutputInfo(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
		MethodSignature ms = (MethodSignature)proceedingJoinPoint.getSignature();
		String operation = ms.getMethod().getAnnotation(Log.class).value();
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();

		String finalOp = operation.trim().length() == 0 ? "" : "[" + operation + "]";

		log.info("执行业务"+ finalOp +",参数:{} ",proceedingJoinPoint.getArgs());
		Object result = proceedingJoinPoint.proceed();
		stopWatch.stop();
		log.info("业务"+ finalOp +"执行完毕: {},耗时{}ms",result,stopWatch.getTotalTimeMillis());
		return result;
	}
}
